<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="搜索名称" prop="searchName">
        <el-input
          v-model="queryParams.searchName"
          placeholder="请输入搜索名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="authList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" fixed="left" />
      <el-table-column
        label="电话"
        align="center"
        prop="phone"
        width="150"
        fixed="left"
      />
      <el-table-column label="头像" align="center" prop="headimgurl">
        <template slot-scope="scope">
          <div v-if="scope.row.headimgurl == null">暂无</div>
          <div v-else>
            <image-preview
              :src="scope.row.headimgurl"
              :width="p_width"
              :height="p_height"
            />
          </div>
        </template>
      </el-table-column>

      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="搜索名称" align="center" prop="searchName" />

      <el-table-column
        label="附近分享"
        align="center"
        prop="nearbyCanShare"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.nearbyCanShare"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'nearbyCanShare')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="附近导航"
        align="center"
        prop="nearbyCanNavigation"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.nearbyCanNavigation"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="
              handleSwitchChange($event, scope.row.id, 'nearbyCanNavigation')
            "
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="附近打电话"
        align="center"
        prop="nearbyCanPhone"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.nearbyCanPhone"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'nearbyCanPhone')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="附近信息"
        align="center"
        prop="nearbySeeCarInfo"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.nearbySeeCarInfo"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="
              handleSwitchChange($event, scope.row.id, 'nearbySeeCarInfo')
            "
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="国控点分享"
        align="center"
        prop="canShare"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.canShare"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'canShare')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="国控点导航"
        align="center"
        prop="canNavigation"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.canNavigation"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'canNavigation')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="国控点打电话"
        align="center"
        prop="canPhone"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.canPhone"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'canPhone')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="国控点信息"
        align="center"
        prop="seeCarInfo"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.seeCarInfo"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'seeCarInfo')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="国控点救援申请"
        align="center"
        prop="canRescueApply"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            class="demo"
            v-model="scope.row.canRescueApply"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="冻结"
            inactive-text="启用"
            :inactive-value="1"
            :active-value="0"
            @change="handleSwitchChange($event, scope.row.id, 'canRescueApply')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rescue:auth:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改救援权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAuth,
  getAuth,
  delAuth,
  addAuth,
  updateAuth,
} from "@/api/rescue/auth";

export default {
  name: "Auth",
  data() {
    return {
      // 图片宽度
      p_width: 40,
      // 图片高度
      p_height: 40,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 救援权限表格数据
      authList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        searchName: null,
        name: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSwitchChange(val, key, field) {
      console.log(val, key, field);
      let params = {
        id: key,
        [field]: val,
      };
      updateAuth(params).then((response) => {
        if (response.code == 200) {
          this.$message.success("修改成功");
        }
      });
    },
    /** 查询救援权限列表 */
    getList() {
      this.loading = true;
      listAuth(this.queryParams).then((response) => {
        this.authList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        nickname: null,
        searchName: null,
        name: null,
        phone: null,
        headimgurl: null,
        nearbyCanShare: null,
        nearbyCanNavigation: null,
        nearbyCanPhone: null,
        nearbySeeCarInfo: null,
        canShare: null,
        canNavigation: null,
        canPhone: null,
        seeCarInfo: null,
        canRescueApply: null,
        status: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加救援权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAuth(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改救援权限";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAuth(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuth(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除救援权限编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAuth(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "rescue/auth/export",
        {
          ...this.queryParams,
        },
        `auth_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
// 是否冻结样式修改
.demo ::v-deep .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
  font-size: 12px !important;
  width: 50px;
}
::v-deep .el-switch__label * {
  font-size: 12px;
}
/*打开时文字位置设置*/
.demo ::v-deep .el-switch__label--right {
  z-index: 1;
  right: 14px;
}
/*关闭时文字位置设置*/
.demo ::v-deep .el-switch__label--left {
  z-index: 1;
  left: 14px;
}
/*显示文字*/
.demo ::v-deep .el-switch__label.is-active {
  display: block;
}
.demo ::v-deep .el-switch__core {
  width: 64px !important;
}
</style>
